package org.wutopia.labcloud.domain.uaa.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserCreateAuthorityInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserAuthorityListOutput;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.UserAuthorityListOutputVO;
import org.wutopia.labcloud.domain.uaa.dao.UserAuthAuthorityDao;
import org.wutopia.labcloud.domain.uaa.dao.entity.UserAuthAuthority;
import org.wutopia.labcloud.domain.uaa.service.UserAuthorityService;
import org.wutopia.labcloud.library.common.base.PageInputVO;
import org.wutopia.labcloud.library.common.base.PageOutputVO;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class UserAuthorityServiceImpl implements UserAuthorityService {

    private UserAuthAuthorityDao userAuthAuthorityDao;

    @Override
    public void createAuthority(UserCreateAuthorityInputVO vo) {

        UserAuthAuthority userAuthAuthority = new UserAuthAuthority();

        userAuthAuthority.setAuthorityName(vo.getAuthorityName());

        userAuthAuthorityDao.insert(userAuthAuthority);

    }

    @Override
    public PageOutputVO<UserAuthorityListOutputVO> list(PageInputVO vo) {

        PageOutputVO<UserAuthorityListOutputVO> outVO = new PageOutputVO<>();


        UserAuthorityListOutputVO outputVO = new UserAuthorityListOutputVO();


        Page<UserAuthAuthority> page = new Page<>(vo.getPage(), vo.getSize());
        Page<UserAuthAuthority> userAuthAuthorityPage = userAuthAuthorityDao.selectPage(page, null);

        List<UserAuthorityListOutput> list = new ArrayList<>();

        List<UserAuthAuthority> records = userAuthAuthorityPage.getRecords();

        for (UserAuthAuthority a : records) {
            UserAuthorityListOutput o = new UserAuthorityListOutput();
            o.setId(a.getId());
            o.setAuthorityName(a.getAuthorityName());
            list.add(o);
        }

        outputVO.setList(list);

        outVO.setSize(vo.getSize());
        outVO.setPage(vo.getPage());
        outVO.setTotalPage(userAuthAuthorityPage.getPages());
        outVO.setTotal(userAuthAuthorityPage.getTotal());
        outVO.setData(outputVO);
        return outVO;
    }
}
